package com.zeta.eduservice.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduTeacher;
import com.zeta.eduservice.entity.vo.TeacherQuery;
import com.zeta.eduservice.service.EduTeacherService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-07-29
 */
@CrossOrigin 
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

	@Autowired
	EduTeacherService teaservice;

	@ApiOperation("登录")
	@PostMapping("login")
	public R login() {
	     return R.ok().data("token","admin");
	     	
	}
	@ApiOperation("登录新")
	@GetMapping("info")
	public R info() {
	    return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
	    	
	}
	@ApiOperation("增加教师")
	@PostMapping("")
	public R addTeacher(@RequestBody EduTeacher eduTeacher) {
		boolean flag = teaservice.save(eduTeacher);
		if(flag)
			return R.ok();
		else
			return R.error();
	}
	@ApiOperation(value = "根据ID删除教师")
	@DeleteMapping("{id}")
	public R removeTeacher(@PathVariable String id) {
		boolean flag = teaservice.removeById1(id);
		 if(flag){
		        return R.ok();
		    }else{
		        return R.error().message("删除失败");
		    }
	}

	@ApiOperation(value = "根据ID修改讲师")
	@PutMapping("{id}")
	public R updateById(
	    @ApiParam(name = "id", value = "讲师ID", required = true)
	    @PathVariable String id,

	    @ApiParam(name = "teacher", value = "讲师对象", required = true)
	    @RequestBody EduTeacher teacher){

	    teacher.setId(id);
	    teaservice.updateById(teacher);
	    return R.ok();
	}
	
	
	@ApiOperation("查询教师")
	@GetMapping("{id}")
	public R findTeacher(@PathVariable String id) {
		EduTeacher byId = teaservice.getById(id);
		return R.ok().data("teacher", byId);
	}
	@ApiOperation("查询所有教师")
	@GetMapping("/findAll")
	public R findAllTeacher() {
		List<EduTeacher> list = teaservice.list(null);
		return R.ok().data("teachers", list);

	}
	@ApiOperation(value = "分页查询教师列表")
	@GetMapping("{page}/{limit}")
	public R pageList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit) {

		Page<EduTeacher> pageParam = new Page<>(page, limit);
		teaservice.page(pageParam, null);
		List<EduTeacher> records = pageParam.getRecords();
		long total = pageParam.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}

	@ApiOperation("条件查询带分页")
	@PostMapping("/pageTeacherCondition/{current}/{limit}")//requestbody必须用 post   required 这个值可以没用
	public R PageTeacherCondition(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) TeacherQuery query) {
		Page<EduTeacher> page = new Page<>(current, limit);
		QueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>();
		if(query!=null) {
			String name = query.getName();
			Integer level = query.getLevel();
			String begin = query.getBegin();
			String end = query.getEnd();
			if(!StringUtils.checkValNull(name)) {
				wrapper.like("name", name);//模糊查询
			}
			if(level!=null) {
				wrapper.eq("level",level);
			}
			if(!StringUtils.checkValNull(begin)) {
				wrapper.ge("gmt_create", begin);
			}
			if(!StringUtils.checkValNull(end)) {
				wrapper.le("gmt_create", end);
			}
		}
	
		
		
		teaservice.page(page, wrapper);
		List<EduTeacher> records = page.getRecords();
		long total = page.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}
	@GetMapping("/getByNumber/{id}/{academy}")
	public R getByNumber(@PathVariable String id,@PathVariable String academy) {
			QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<EduTeacher>();
			queryWrapper.eq("number", id);
			queryWrapper.eq("academy", academy);
		EduTeacher one = teaservice.getOne(queryWrapper);
		return R.ok().data("teacher",one);
	}
	@ApiOperation("条件查询带分页")
	@PostMapping("/pageTeacherConditionByclass/{current}/{limit}")//requestbody必须用 post   required 这个值可以没用
	public R PageTeacherConditionByclass(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) TeacherQuery searchObj) {
		Page<EduTeacher> page = new Page<>(current, limit);
		List<EduTeacher> byclass = teaservice.getByclass(page,searchObj);
		long total = page.getTotal();
		return R.ok().data("total", total).data("rows", byclass);
	}


}
